<?php

/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework										|
 * | Version 1.0																|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>					|
 * | Blog: http://mcezzare.blogspot.com.br										|
 * |																			|
 * | This program is part of PHPKiisf, and is free software: you can			|
 * | redistribute it and/or modify it under the terms of the GNU 				|
 * | General Public License as published by										|
 * | the Free Software Foundation, either version 3 of the License, or			|
 * | (at your option) any later version.										|
 * |																			|
 * | This program is distributed in the hope that it will be useful,			|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of				|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the				|
 * | GNU General Public License for more details.								|
 * | You should have received a copy of the GNU General Public License			|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.		|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>			|
 * +----------------------------------------------------------------------------+
 * | Contributors																|
 * |																			|
 * +----------------------------------------------------------------------------+
 *
 */
/**
 * @package	Lib
 * @access	public
 * @since	2013-03-08
 * @author	Mario Cezzare <mcezzare@gmail.com>
 */

/**
 * Common project functions in class methods
 *
 */
class utils {

	public static $message ;
	public static $message_style = array() ;
	public static $bg_color ;

	/**
	 *
	 */
	public function __construct() {
		self::load() ;
	}

	/**
	 *
	 */
	public static function init() {
		self::load() ;
	}

	/**
	 *
	 */
	public static function load() {
		self::$message_style = array(
			"ERROR" => array(
				'color' => 'white' ,
//								'bgcolor' => 'rgb(245, 59, 0)' ,
				'bgcolor' => '#C33' ,
				'class_icon' => 'icon-thumbs-down' ,
			) ,
			"INFO" => array(
				'color' => 'black' ,
				'bgcolor' => 'cyan' ,
				'class_icon' => 'icon-ok-circle' ,
			) ,
			"WARNING" => array(
				'color' => 'black' ,
				'bgcolor' => 'yellow' ,
				'class_icon' => 'icon-exclamation-sign' ,
			) ,
				) ;
	}

	/**
	 * Show a object between tags pre
	 * 
	 * @param default_model $object
	 * @param type $line
	 * @param type $file
	 * @param type $print_id if put  id tag on pre 
	 * @return type
	 */
	public static function show_object( $object , $line = "" , $file = "" , $print_id = null ) {

		$position = $line != "" && $file != "" ? "-->Called at  line $line of $file<br> " : null ;

		print "$position " ;

		if ( $print_id === null ) {
			echo "<pre style=\"border:solid 1px #EA9C5C;  padding:3px; color:#000000; background-color:#ededed \">" ;
		} else {
			echo "<pre id=\"$print_id\" style=\"border:solid 1px #EA9C5C;  padding:3px; color:#000000; background-color:#ededed \">" ;
		}


		if ( $object instanceof default_model ) {
			var_export( $object->scaffold() ) ;
		} else {
			var_export( $object ) ;
		}


		echo "</pre>" ;
		echo "<hr size=\"1\" width=\"100%\">" ;
		return ;
	}

	/**
	 *
	 * @param message $message
	 */
	public static function inform_user( $message , $include_templates = false ) {

//				$msg = self::prepare_message( $message ) ;
//				var_dump( $message ) ;

		gui_controller::print_div_pre_message( $message , $include_templates ) ;
		return ;
	}

	/**
	 *
	 * @param type $template
	 * @return boolean
	 */
	public static function check_template( $template ) {
		if ( ( $template !== null ) ) {
			if ( file_exists( $template ) ) {
				return true ;
			}
			return false ;
		}
	}

	/**
	 *
	 * @param message/string/array $message
	 * @return \message
	 */
	public static function prepare_message( $message ) {
		$msg_object = new message() ;
		if ( is_array( $message ) ) {
			$msg_object->create_message( $message ) ;
		}

		if ( $message instanceof message ) {
			$msg_object = $message ;
		}

		return $msg_object ;
	}

	/**
	 *
	 * @param message $var
	 * @return type
	 */
	public static function object_to_string( $var ) {
		$return = "" ;
		if ( is_array( $var ) ) {
			foreach ( $var as $key => $value ) {
				$return.= "$key : $value<br>\n" ;
			}
			return $return ;
		}
		if ( $var instanceof message ) {
			return $var->__toString() ;
		}

		return $return ;
	}

	/**
	 * Create a list of parameters to append on current object
	 *
	 * @param type $array Description
	 * @param type $language
	 * HTML key="value"
	 *
	 */
	public static function array_to_params( $array , $object_type = null , $language = "HTML" ) {
		$return = "" ;
		if ( $language == "HTML" ) {
			if ( is_array( $array ) ) {
//								if ( $object_type == null ) {
//										foreach ( $array as $key => $value ) {
//												$return.= sprintf( " %s=\"%s\"" , $key , $value );
//										}
//										return $return;
//								}
				//else
				switch ( $object_type ) {
					case "select":
//												var_dump( $array );
//												exit;
						foreach ( $array as $key => $value ) {
							$return.= sprintf( "<option value=\"%s\">%s</option>\n" , $value , $key ) ;
						}
						break ;
					default :
						foreach ( $array as $key => $value ) {
							$return.= sprintf( " %s=\"%s\" " , $key , $value ) ;
						}
						break ;
				}
			}
		}
		return $return ;
	}

	/**
	 * Show an array user friendly and prepare some array configurations to display values
	 *
	 * @param array $array array to analyze
	 * @param array $options Example = array( "test" => true ) ;
	 *
	 */
	public static function dump_array( $array , $options = null ) {
		$filter = false ;
		if ( is_array( $array ) ) {
			$result_test_file = null ;
			foreach ( $array as $key => $value ) {
//								if ( in_array( "section_only" , $options ) || array_keys( $options , "section_only" ) ) {
//										$filter = true ;
//								}
//
//								if ( $filter == true && $options{"section_only"} == $key ) {
				$force_br = false ;
//								if ($filter==true &&  $options{"section_only"} == $key){
				print sprintf( "<br>Section: <b>%s</b><br>" , $key ) ;
				print "<ul>" ;
				while ( list( $param , $val ) = each( $value ) ) {
					if ( $options !== null && is_array( $options ) ) {
						/* minifying.. */
//												var_dump( $options ) ;

						if ( in_array( "test" , $options ) || array_keys( $options , "test" ) ) {
							$test = true ;
						} elseif ( $options === true ) {
							$test = true ;
						} else {
							$test = false ;
						}

//												utils::finish_program( $test , get_called_class() ) ;

						$warning_message = "" ;
						$default_defined_message = " <b style='font-size:80%;color: #000;'>[Using default value.Change it.]</b>" ;
						/* perform tests if exists */
						if ( $test ) {
							switch ( $key ) {
								case "TEMPLATES" :
									$testname = "CHECK_FILE" ;
									break ;

								case "DIRS" :
									$testname = "CHECK_DIR" ;
									break ;

								case "DATABASE" :
									$testname = "CHECK_VALUE" ;
									/* check if are the default settings */
									$warning_message .= ($param == "DB_NAME" && $val == "name_db") ? $default_defined_message : null ;
									$warning_message .= ($param == "DB_HOST" && $val == "host") ? $default_defined_message : null ;
									$warning_message .= ($param == "DB_USER" && $val == "user") ? $default_defined_message : null ;
									$warning_message .= ($param == "DB_PASS" && $val == "pass") ? $default_defined_message : null ;

									break ;

								default:
									$testname = "CHECK_VALUE" ;
									break ;
							}
							if ( $key == "DATABASE" ) {
								$result_test_file = utils::perform_test( $val , $testname , $warning_message , true ) ;
							} else {
								$result_test_file = utils::perform_test( $val , $testname , $warning_message , false ) ;
							}
						}
					}
					if ( $key == "DATABASE" ) {
						print("<li>Hidden for security</li>" ) ;
						printf( "<li class='destak_focus'><b>%s</b> => <u>%s</u> %s</li>" , $param , md5( $val ) , gui_controller::print_div_results( $result_test_file , null , $force_br ) ) ;
					} else {
						printf( "<li class='destak_focus'><b>%s</b> => <u>%s</u> %s</li>" , $param , $val , gui_controller::print_div_results( $result_test_file , null , $force_br ) ) ;
					}
				}
				print "</ul>" ;
			}
		}
//				}
		else {
			printf( "not array:%s" , $array ) ;
		}
	}

	public static function perform_test( $val , $test_type = "CHECK_VALUE" , $warning_message = null , $hide_value = false ) {
		$defined_tests = array(
			"CHECK_FILE" => "File" ,
			"CHECK_DIR" => "Dir" ,
			"CHECK_VALUE" => "Value: " . $val ,
				) ;
		if ( $hide_value ) {
			$defined_tests{"CHECK_VALUE"} = "Value: " . substr( crypt( $val ) , 0 , 5 ) ;
		}
		$result_test_file = array(
			"OPERATION" => $test_type ,
			"RESULT" => FALSE ,
			"MESSAGE" => $test_type . " not exists or not defined" ,
				) ;
		;
		if ( $test_type !== null && array_key_exists( $test_type , $defined_tests ) ) {
			if ( $test_type == "CHECK_FILE" || $test_type == "CHECK_DIR" ) {
				$result_test_file = array(
					"OPERATION" => $test_type ,
					"RESULT" => FALSE ,
					"MESSAGE" => $defined_tests{$test_type} . " not exists or not defined" ,
						) ;
				if ( utils::check_template( $val ) ) {
					$result_test_file{"RESULT"} = TRUE ;
					$result_test_file{"MESSAGE"} = $defined_tests{$test_type} . " exists" ;
				}
			}
			if ( $test_type == "CHECK_VALUE" ) {
				if ( is_null( $val ) ) {
					$result_test_file{"RESULT"} = FALSE ;
					$result_test_file{"MESSAGE"} = $defined_tests{$test_type} . " not defined" ;
				} else {
					$result_test_file{"RESULT"} = TRUE ;
					$result_test_file{"MESSAGE"} = $defined_tests{$test_type} . " defined" ;
				}
			}
		}
		if ( $warning_message !== null ) {
			$result_test_file{"MESSAGE"} = $result_test_file{"MESSAGE"} . $warning_message ;
		}
		return $result_test_file ;
	}

	public static function finish_program( $options = null , $caller = null ) {

		if ( $options !== null ) {
			if ( is_array( $options ) ) {
				print_r( $options ) ;
			} else {
				utils::show_object( $options ) ;
			}
		}
		$hr = "<hr>" ;
		printf( "%s Program Finished.<br>\nCalled from :%s %s" , $hr , $caller , $hr ) ;

		die() ;
		exit() ;
	}

	public static function export_array( $array , $model_section ) {
		$return = "" ;
		if ( is_array( $array ) ) {

			if ( $model_section == "table" ) {
				foreach ( $array[0] as $key => $value ) {
					$return.= "\t\t\t\t\t\"$key\" => \"$value\" ,\n" ;
				}
				return $return ;
			}

			foreach ( $array as $array_multiple ) {
				$return.= "\t\t";
				foreach ( $array_multiple as $key => $value ) {
					$return.= "\t\t\t\t\t\"$key\" => \"$value\" ,\n" ;
				}
			}
		}
		return $return ;
	}

}

?>